package com.sky.com.sky.web.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    DishService dishService;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 菜品添加
     *
     * @param dishDTO
     * @return
     **/
    @PostMapping
    public Result saveDishWithFloavor(@RequestBody DishDTO dishDTO){
       dishService.saveDishWithFlavor(dishDTO);

       //清理缓存
        redisTemplate.delete("DISH:"+dishDTO.getCategoryId());

        return Result.success();
    }


    /**
     * 根据id查询菜品和菜品口味
     *
     * @param id
     * @return
     **/
   @GetMapping("/{id}")
   public Result getById(Long id){
       DishVO dishVO = dishService.getById(id);
       return Result.success(dishVO);
   }


    /**
     * 修改菜品和对应的口味
     *
     * @param dishDTO
     * @return
     **/
    @PutMapping
    public Result updateById(@RequestBody DishDTO dishDTO){
        dishService.updateById(dishDTO);
        //清理缓存
        Set dishKey = redisTemplate.keys("DISH*");
        redisTemplate.delete(dishKey);

        return Result.success();
    }


    /**
     * 菜品批量删除
     *
     * @param ids
     * @return
     **/
    @DeleteMapping
    public Result delete(Long[] ids) {
        List<Dish> list = dishService.listByIds(ids);
        dishService.deleteBatch(ids);
        //清理缓存
        for (Dish dish : list) {
            redisTemplate.delete("DISH_"+dish.getCategoryId());
        }
        return Result.success();
    }





    //查询菜品列表
    @GetMapping("/list")
    public Result getList(DishPageDTO dishPageDTO){
        List<DishVO> voList = dishService.getList(dishPageDTO);
        return Result.success(voList);
    }


    /**
     * 菜品分页查询
     *
     * @param dishPageDTO
     * @return
     **/
    @GetMapping("/page")
    public Result getPage(DishPageDTO dishPageDTO){
        PageResult pageResult = dishService.getPage(dishPageDTO);
        return Result.success(pageResult);
    }

    /**
     * 菜品起售 停售
     *
     * @param status
     * @param id
     * @return
     **/
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable("status") Integer status,
                              @RequestParam Long id){
        dishService.startOrStop(status,id);
        //清理缓存
        Set dishkey = redisTemplate.keys("DISH*");
        redisTemplate.delete(dishkey);
        return Result.success();
    }


    /**
     * 根据分类ID查询起售菜品列表
     *
     * @param categoryId
     * @return
     **/
    @GetMapping("/list")
    public Result listByCid(Long categoryId) {
        //封装查询条件
        DishDTO dishDTO = DishDTO.builder()
                .categoryId(categoryId)
                .status(1)
                .build();
        return Result.success(dishService.listByCid(dishDTO));
    }


}

